package cn.slipi.pan.common.response;

import cn.slipi.pan.common.constants.ExceptionConstants;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

/**
 * 返回结果
 *
 * @author LEE
 */
@Data
@Accessors(chain = true)
public class ApiResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 错误信息
     */
    private String error;

    /**
     * 无参构造
     */
    public ApiResult() {
    }

    /**
     * code构造
     */
    public ApiResult(Integer code) {
        this(code, null);
    }

    /**
     * code/message构造
     */
    public ApiResult(Integer code, String message) {
        this(code, message, null);
    }

    /**
     * code/message/data构造
     */
    public ApiResult(Integer code, String message, T data) {
        this(code, message, data, null);
    }

    /**
     * code/message/data/error构造
     */
    public ApiResult(Integer code, String message, T data, String error) {
        setCode(code);
        setMessage(message);
        setData(data);
        setError(error);
    }

    public static ApiResult<?> success() {
        ApiResult<?> apiResult = new ApiResult<>();
        apiResult.setCode(ExceptionConstants.SUCCESS.getCode());
        apiResult.setMessage(ExceptionConstants.SUCCESS.getMessage());
        apiResult.setData(null);
        return apiResult;
    }

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> apiResult = new ApiResult<T>();
        apiResult.setCode(ExceptionConstants.SUCCESS.getCode());
        apiResult.setMessage(ExceptionConstants.SUCCESS.getMessage());
        apiResult.setData(data);
        return apiResult;
    }

    public static <T> ApiResult<T> fail(ExceptionConstants constants) {
        ApiResult<T> apiResult = new ApiResult<T>();
        apiResult.setCode(constants.getCode());
        apiResult.setMessage(constants.getMessage());
        apiResult.setData(null);
        return apiResult;
    }

    public static <T> ApiResult<T> fail(ExceptionConstants constants, T data) {
        ApiResult<T> apiResult = new ApiResult<T>();
        apiResult.setCode(constants.getCode());
        apiResult.setMessage(constants.getMessage());
        apiResult.setData(data);
        return apiResult;
    }

    public static <T> ApiResult<T> fail(ExceptionConstants constants, T data, String message) {
        ApiResult<T> apiResult = new ApiResult<T>();
        apiResult.setCode(constants.getCode());
        apiResult.setMessage(message);
        apiResult.setData(data);
        return apiResult;
    }


}
